﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "PushButton.h"
#include "../node/Sprite.h"
#include "../base/Debuger.h"
#include "../node/SpriteFrame.h"
#include "../base/EventMouse.h"
#include "../render/Camera.h"
#include "../ui/Scale9Image.h"
#include "../base/EventResize.h"
#include "../base/EventWidgetMove.h"

using namespace std;

namespace SkyEngine2d
{
	
	PushButton::~PushButton()
	{
	
	}
	
	PushButton* PushButton::create(Widget* parent /*= nullptr*/)
	{
		auto p = makeWidgetPtr(parent);
		if (p->init())
		{
			return p;
		}
	
		return nullptr;
	}
	
	PushButton* PushButton::create(const std::wstring & normal_picture, const Rect &cap_insets /*= Rect::Zero*/, Widget* parent /*= nullptr*/)
	{
		auto p = makeWidgetPtr(parent);
		if (p->init(normal_picture, cap_insets))
		{
			return p;
		}
		return nullptr;
	}
	
	PushButton* PushButton::create(const SpriteFrameSharedPtr & sprite_frame, const Rect &cap_insets /*= Rect::Zero*/, Widget* parent /*= nullptr*/)
	{
		auto p = makeWidgetPtr(parent);
		if (p&&p->init(sprite_frame, cap_insets))
		{
			return p;
		}
		return nullptr;
	}
	
	void PushButton::setClickedListener(const MouseClickedCallback & call)
	{
		m_clickedCall = call;
	}
	
	void PushButton::draw(IRender* render, const Matrix4X4& transform)
	{
		if (m_textRender)
		{
			m_textRender->draw(render, Matrix4X4::IdentityMat);
		}
	
		if (m_currentImage)
		{
			DirectX::XMMATRIX mvp = DirectX::XMMatrixMultiply(transform, getCamera()->getViewProjectMat());
			m_currentImage->draw(render, mvp);
		}
	}
	
	PushButton::PushButton(Widget* parent /*= nullptr*/)
		:AbstractButton(parent)
	{
	
	}
	
	bool PushButton::init(const std::wstring & normal_picture, const Rect & cap_insets)
	{
		if (!AbstractButton::init())
		{
			return false;
		}
	
		this->setNormalImage(normal_picture, cap_insets);
	
		return true;
	}
	
	bool PushButton::init(const SpriteFrameSharedPtr & sprite_frame, const Rect & cap_insets)
	{
		if (!AbstractButton::init())
		{
			return false;
		}
		
		this->setBackgroundImage(sprite_frame, cap_insets);
	
		return true;
	}
	
	bool PushButton::init()
	{
		if (!AbstractButton::init())
		{
			return false;
		}
	
		this->setGeometry(Rect(0, 0, this->getDefultWidth(), this->getDefultHeight()));
	
		return true;
	}
	
	void PushButton::onMouseReleaseEvent(EventMouse * event)
	{
		AbstractButton::onMouseReleaseEvent(event);
		
		//触发单击回调
		if (m_clickedCall)
		{
			m_clickedCall(event);
		}
	}
	
	
	int PushButton::getDefultWidth() const
	{
		return 150;
	}
	
	int PushButton::getDefultHeight() const
	{
		return 70;
	}
	
	void PushButton::onResizeEvent(EventResize * e)
	{
		AbstractButton::onResizeEvent(e);
	
		auto & size = e->getNewSize();
	
		if (m_textRender)
		{
			m_textRender->setDrawRect(Rect(this->getAbsPos(), size));
		}
	}
	
	void PushButton::onMoveEvent(EventWidgetMove * e)
	{
		AbstractButton::onMoveEvent(e);
	
		if (m_textRender)
		{
			m_textRender->setDrawRect(Rect(this->getAbsPos(), m_geometry.size));
		}
	}
	
}






